<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\models\category\Category;

/* @var $this yii\web\View */
/* @var $searchModel common\models\category\CategorySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', '栏目管理');
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $this->beginBlock('siderbar'); ?>
<?= $this->render('//layouts/cms-menu') ?>
<?php $this->endBlock(); ?>
<script type="text/javascript" src="<?= yii::getAlias('@web');?>/js/jquery-2.2.3.min.js"></script>
<div class="term-index">
    <p>
        <?= Html::a(Yii::t('app', 'Create'), ['create'], ['class' => 'btn btn-success']) ?>

<div id="w0" class="grid-view"><div class="summary">第<b>1-<?=$num?></b>条，共<b><?=$num?></b>条数据.</div>
<table class="table table-striped table-bordered" id="content_list">
<thead>
<tr>
<th>ID</th>
<th><a href="<?= yii\helpers\Url::to(['category/index/','sort'=>'name']);?>" data-sort="name">栏目</a></th>
<th><a href="<?= yii\helpers\Url::to(['category/index/','sort'=>'status']);?>" data-sort="status">导航显示</a></th>
<th width="200">操作</th>
</tr>
</thead>
<tbody>

<?php foreach($data as $key => $val) : ?>

<tr data-key="<?=$val['id']?>" id=<?php echo 'cat_'.$val['id'];?>>
<td style="width:20%"><?=$val['id']?></td>

<td>
<a href="javascript:;" class="cat_btn" data-url="<?= yii\helpers\Url::to(['category/children/']);?>/<?=$val['id']?>?layer=1">+</a> <?=$val['name']?>
</td>

<td><span class="label <?php if($val['status']==1){echo 'label-success';}elseif($val['status']==0){echo 'label-warning';}elseif($val['status']==-1){echo 'label-danger';}?>"><?php if($val['status']==1){echo '是';}elseif($val['status']==0){echo '否';}elseif($val['status']==-1){echo '删除';}?></span></td>

<td>

<a href="<?= yii\helpers\Url::to(['category/view/','id'=>$val['id']]);?>" title="查看">&nbsp;&nbsp;<span class="glyphicon glyphicon-eye-open" >查看</span></a>

<a href="<?= yii\helpers\Url::to(['category/update/','id'=>$val['id']]);?>" title="修改">&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil">修改</span></a>

<?php if(category::isDel($val['id'])) $data = "该栏目下有子栏目或者有内容,不能删除！"; else $data = "你确定要删除该栏目吗？"; ?>
<a href="<?= yii\helpers\Url::to(['category/delete/','id'=>$val['id']]);?>" title="删除" data-confirm="<?=$data?>" data-method="post">&nbsp;&nbsp;<span class="glyphicon glyphicon-trash" >删除</span></a></td>


</tr>

<?php endforeach;?>

</tbody></table>
</div>
<script type="text/javascript">
    $(function(){
        //分类展开与收起
        $('#content_list').delegate('.cat_btn', 'click',function(){
			var tr_val = '';
			var tr_str = '';
            var tr_id = $(this).closest('tr').attr('id');
			var id = $(this).closest('tr').attr('data-key');
            if($(this).hasClass('cat_down')) {
                $(this).removeClass('cat_down').html('+'); 
				$.getJSON("<?= yii\helpers\Url::to(['category/getchildren/']);?>/"+id,function(data){
					$.each(data, function(i, item){
						$('.cat_'+item.parent_id+'_children').remove();
					});
				});
                $('.loading').remove();
            } else {
                $(this).addClass('cat_down').html('--');
                var url = $(this).attr('data-url');
                $('#'+tr_id).after('<tr class="tb_list loading"><td>loading...</td></tr>');
                var active_id = tr_id;
                $.getJSON(url,function(data){
                    $('.loading').remove();
                    $.each(data, function(i, item){
                        var space = '';
                        for(i=1;i<item.layer;i++) {
                            space += '&nbsp;&nbsp;&nbsp;&nbsp;';
                        }
						if(item.status==1){
							tr_val = 'label-success';
							tr_str = '是';
						}else if(item.status==0){
							tr_val = 'label-warning';
							tr_str = '否';
						}else if(item.status==-1){
							tr_val = 'label-danger';
							tr_str = '删除';
						}
						$showhas = "-";
                        if(item.haschildren >0 ){
                            $showhas = "+"
                        }
                        $showcontent = "";
                        if(item.haschildren ==0 ){
                            $showcontent = '<a href="<?= yii\helpers\Url::to(['articles/contentindex/']);?>/'+item.id+'" title="查看列表">&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil">查看列表</span></a>'
                        }

                        var html = '<tr data-key="'+item.id+'" id=cat_'+item.id+' class="cat_children tb_list cat_'+item.parent_id+'_children">'
                            + '<td>'+space+'├──'+item.id+'</td>'
                            + '<td>'+space+'<a href="javascript:;" class="cat_btn" data-url="<?= yii\helpers\Url::to(['category/children/']);?>/'+item.id+'?layer='+item.layer+'" >'+$showhas+'</a>'+ item.name+'</td>'
                            + '<td>'+'<span class="label '+tr_val+'">'+tr_str+'</span></td>'
                            + '<td>'
                            + '<a href="'+item.view_url+'" title="查看">&nbsp;&nbsp;<span class="glyphicon glyphicon-eye-open" >查看</span></a><a href="'+item.update_url+'" title="修改">&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil">修改</span></a><a href="'+item.delete_url+'" title="删除" data-confirm="'+item.deltrue+'" data-method="post">&nbsp;&nbsp;<span class="glyphicon glyphicon-trash" >删除</span></a>'+$showcontent
                            + '</td>'
                            + '</tr>';
                            $('#'+active_id).after(html);
                            active_id = 'cat_'+item.id;
                    }); 
                }); 
            }
        });
    });
</script>
</div>